package com.wx.mall.user.controller;


import com.wx.mall.common.api.CommonPage;
import com.wx.mall.common.api.CommonResult;
import com.wx.mall.common.api.ResultCode;
import com.wx.mall.common.exceptions.ApiAssert;
import com.wx.mall.common.utils.DataResult;
import com.wx.mall.common.vo.user.UserReqVo;
import com.wx.mall.user.dto.DisUseDto;
import com.wx.mall.user.entity.User;
import com.wx.mall.user.service.IUserService;
import com.wx.mall.user.vo.DisUseVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author j
 * @since 2020-03-20
 */
@Api(tags = "UserController",description = "用户列表")
@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {

    @Resource
    IUserService service;

    @ApiOperation("查询全部数据")
    @GetMapping("/listAll")
    public DataResult getListAll(){

        List<User> list=service.list();
        return DataResult.success(list);
    };

    @ApiOperation("根据ID查询数据")
    @GetMapping("/listById")
    public DataResult getListById(Long userId){

        User list=service.getListById(userId);
        return DataResult.success(list);
    };

    @ApiOperation("添加一条数据")
    @PostMapping("/addList")
    public DataResult addList(User user){
        boolean success=service.save(user);


        return success?DataResult.success():DataResult.getResult(1,"添加失败");

    };
    @ApiOperation("根据Id修改数据s")
    @PutMapping("/updateListById")
    public DataResult setListById(User user){
        boolean success=service.updateById(user);

        return success?DataResult.success():DataResult.getResult(1,"修改失败");

    };
    @ApiOperation("根据Id批量修改数据或删除数据")
    @PutMapping("/updateList")
    public DataResult setListByIdList(UserReqVo vo){
        System.out.println(vo);
        int success= service.setListByIdList(vo);

        return success==1?DataResult.success():DataResult.getResult(1,"修改失败");

    };


    @ApiOperation("分页后根据某字段排序：全部数据")
    @GetMapping("/listByOder")
    public CommonResult<CommonPage<User>> getListByOder(Integer pageNum, Integer pageSize, int sort){

        System.out.println(pageNum);
        if (pageNum == null || pageSize == null) {
            ApiAssert.fail(ResultCode.VALIDATE_FAILED); // 直接抛异常，使用enum
        }
        if (pageNum <= 0) {
            ApiAssert.fail("无效页码"); // 直接抛异常，使用自定义的message
        }
//        0为会员等级，1为消费金额，2为有效订单数量
        List<User> list=service.getListByOder(pageNum,pageSize,sort);
        return CommonResult.success(CommonPage.restPage(list));
    };

    @ApiOperation("根据筛选条件查询全部的分页数据")
    @GetMapping("/getListByUser")
    public CommonResult<CommonPage<User>> getListByUser(Integer pageNum, Integer pageSize, Long userNumber,
                                                        String userName, String userSource,
                                                        Integer userMsId, String userJob,
                                                        Date userCreateTime,int sort){

        List<User> list=service.getListByUser(pageNum,pageSize,userNumber,userName,userSource,userMsId,userJob,userCreateTime,sort);
        return CommonResult.success(CommonPage.restPage(list));
    };




    @ApiOperation("获取到用户优惠券总计数数据")
    @GetMapping("/getTotal")
    public CommonResult<CommonPage<DisUseVo>> getTotal(DisUseDto dto){

        System.out.println(dto);
        if (dto.getPageNum() == null || dto.getPageSize() == null) {
            ApiAssert.fail(ResultCode.VALIDATE_FAILED); // 直接抛异常，使用enum
        }
        if (dto.getPageNum() <= 0) {
            ApiAssert.fail("无效页码"); // 直接抛异常，使用自定义的message
        }

        CommonPage<DisUseVo> list=service.getTotal(dto);

        return CommonResult.success(list);

    };


    @ApiOperation("根据ID查询数据和优惠券记录")
    @GetMapping("/listByIdTwo")
    public CommonResult getListByIdTwo(Long userId){

        DisUseVo list=service.getListByIdTwo(userId);
        return CommonResult.success(list);
    };


}

